<?php
/* 
 * 教练排版，时间段选择控件
 */
namespace Admin\Controller;
class TimeSlotController extends ComController{
	//时间段名称
	public function index(){
		$sc_id = session('uid');
		if($sc_id == ''){
			echo json_encode(array('res'=>0,'msg'=>'您尚未登录，请登录'));
		}
		$slot = M('time_slot');
		$data = $slot->where("school_id = $sc_id and status=1")->select();
		$this->assign('data',$data);
		$this->display();
	}
	//时间段显示列表
	public function detail(){
		$slot_id = I('get.id','','intval');
		if(empty($slot_id)){
			$res='';
		}else{
			$time = M('appointment_time');
			$res = $time->where("slot_id = $slot_id")->select();
			$this->assign('time',$res);
			$this->display();
		}
	}
	//时间段添加
	public function  add(){
		if(empty($_POST)){
			$this->display();
			
		}else{
			$data['slot_name'] = I('post.site','','strip_tags');
			$time=I('post.time','','');
			$price = I('post.unit_price','','');
			$data['add_time']=time();
			$data['school_id']=session('uid');
			if(empty($data['school_id'])){
				$this->error('您尚未登录，请登录',U('Login/index'));die;
			}
			$slot = M('time_slot');
			$res =$slot->data($data)->add();
			if($res === false){
				$this->error('添加失败，请重新添加',U('TimeSlot/add'));die;
			}else{
				foreach ($time as $k=>$v){
					if(!empty($v)){
						$array[$k]['time'] =$v;
						$array[$k]['price']=$price[$k];
						$array[$k]['slot_id']=$res;
					}else{
							
					}
				}
			}
			$app_time = M('appointment_time');
			$result = $app_time->addAll($array);
			if($result === false){
				$this->error('添加失败，请重新添加',U('TimeSlot/add'));
			}else{
				$this->success('添加成功',U('TimeSlot/index'));
			}
		}
	}
	//删除
	public function delete(){
		$where = '';
		$id = I('get.id','','intval');
		if(empty($id)){
			$this->error('参数错误，请重新选择',U('TimeSlot/index'));
		}else{
			$where="id = $id ";
		}
		
		$sc_id = $_SESSION['uid'];
		if(empty($sc_id)){
			$this->error('您尚未登录，请登录',U('Login/index'));
		}else{
			$where.=" and school_id = $sc_id";
		}
		$data['status'] = 0;
		$data['update_time'] = time();
		$time_slot = M('time_slot');
		$res = $time_slot->where($where)->save($data);
		if($res === false){
			$this->error('删除失败，请重新操作',U('index'));
		}else{
			$this->success('删除成功',U('index'));
		}
	}
	//编辑
	public function edit(){
		$id = I('get.id','','intval');
		if(!empty($id)){
			/// 时间段分类
			$time_slot = M('time_slot');
			$slot_name = $time_slot->field('slot_name')->where("id = $id")->find();
			//var_dump($slot_name);
			//具体时间段信息
			$app_time = M('appointment_time');
			$where = "slot_id = $id";
			$date = $app_time->where($where)->select();
			$this->assign('slot_name',$slot_name);
			$this->assign('date',$date);
			$this->display();
		}else{
			//保存编辑后的数据
			
		}
	}
	
	/* 公共方法 */
	public function selectSlot(){
		$sc_id = session('uid');
		if($sc_id == ''){
			echo json_encode(array('res'=>0,'msg'=>'您尚未登录，请登录'));
		}
		$slot = M('time_slot');
		$data = $slot->where("school_id = $sc_id and status =1 ")->select();
		echo returnAjax($data);
	}
	public function appTime(){
		$slot_id = I('post.slot_id','','intval');
		$app_time = M('appointment_time');
		$data = $app_time->where("slot_id = $slot_id and status = 1")->select();
		echo returnAjax($data);
	}

}